// packageC/o2o/o2oGroupActivitylist/o2oGroupActivitylist.js
let app = getApp();
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywords:'',
    store_id: 0,
    activeName: 0,
    titleArr: ['全部活动', '未开始', '活动中', '已结束'],
    dataList: [],
    
    showEndP: false,
    cloneIndex: 0,
    has_many_level: {},
    activeItem: {},
    activeID:0,
    posterShow:false,

    showShare: false,
    options: [
      { name: '分享好友', icon: 'wechat', openType: 'share' },
      { name: '分享海报', icon: 'poster' },
    ],

    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.store_id) {
      this.setData({
        store_id: options.store_id
      });
    }
    this.setNewArr();
  },
  setNewArr() {
    this.data.titleArr.forEach((val, index) => {
      this.data.dataList.push({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    console.log("dataList:::", this.data.dataList);
    this.handleClick();
  },
  clickShare(e){
    this.setData({
      activeItem:e.target.dataset.item,
      activeID:e.target.dataset.item.id,
      showShare:true
    });
  },
  onClose(){
    this.setData({
      showShare:false
    });
  },
  onSelect(event){
    if(event.detail.name=='分享海报'){
      this.setData({
        posterShow:true
      });
    }
  },
  tabClick(e){
    this.setData({
      activeName:e.detail.index
    });
    this.handleClick();
  },
  handleClick() {
    let that = this;
    if (this.data.dataList[this.data.activeName].list.length > 0) {
      return;
    }
    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.index");
    app._getNetWork({
      url: urlStr,
      data: {
        status: this.data.activeName,
        store_id: this.data.store_id,
        page:this.data.page,
        title: this.data.keywords
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['dataList[' + that.data.activeName + '].isLoadMore']: true,
            ['dataList[' + that.data.activeName + '].total_page']: res.data.last_page,
            ['dataList[' + that.data.activeName + '].list']: res.data.data
          });
          for (let index = 0; index < that.data.dataList[that.data.activeName].list.length; index++) {
            let start_time = util.formatTime(new Date(that.data.dataList[that.data.activeName].list[index].start_time * 1000));
            let end_time = util.formatTime(new Date(that.data.dataList[that.data.activeName].list[index].end_time * 1000));
            that.setData({
              ['dataList[' + that.data.activeName + '].list[' + index + '].start_time']: start_time,
              ['dataList[' + that.data.activeName + '].list[' + index + '].end_time']: end_time,
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onSearch(){
    this.setData({
      ['dataList[' + this.data.activeName + '].list']: []
    });
    this.handleClick();
  },
  onChange(e) {
    this.setData({
      keywords: e.detail,
    });
  },
  closeEndP() {
    this.setData({
      showEndP: false
    });
  },
  endGrouplist(e) {
    this.setData({
      cloneIndex: e.currentTarget.dataset.index,
      has_many_level: e.currentTarget.dataset.item.has_many_level,
      showEndP: true
    });
  },
  toDataStatistics(e) {
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: '/packageC/o2o/groupDataStatistics/groupDataStatistics?id=' + id
      });
    }
  },
  toOpenGroupList(e) {
    let id = e.currentTarget.dataset.id;
    if (id) {
      wx.navigateTo({
        url: '/packageC/o2o/openGroupList/openGroupList?id=' + id+'&store_id=' + this.data.store_id
      });
    }
  },
  createActivity(){
    wx.navigateTo({
      url: '/packageC/o2o/createGroup/createGroup?store_id=' + this.data.store_id
    });
  },
  editActivity(e){
    let _id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/o2o/createGroup/createGroup?store_id=' + this.data.store_id+'&id=' + _id
    });
  },
  closelevel(e){
    let {num,id,index} = e.currentTarget.dataset;
    let that = this;
    wx.showModal({
      title: '提示',
      content: `是否手动结束${num}人团`,
      success (res) {
        if (res.confirm) {
          that.closelevelFun(num,id,index);
        } else if (res.cancel) {
          console.log('用户点击取消');
        }
      }
    });
  },
  closelevelFun(num,id,index){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.fight-groups.frontend.store.fight-groups-store.close-level");
    app._getNetWork({
      url: urlStr,
      data: {
        id: id
      },
      success: function (resdata) {
        var res = resdata.data;
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 1000
        });
        if (res.result == 1) {
          that.setData({
            ['dataList[' + that.data.activeName + '].list['+ that.data.cloneIndex +'].has_many_level['+ index+'].status']: 2,
            ['has_many_level[' + index+'].status']: 2
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  _getMoreData() {
    let that = this;
    this.setData({
      ['dataList[' + that.data.activeName + '].isLoadMore']: false
    });
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return;
    } else {
      this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;
      let urlStr = app.getNetAddresss('plugin.fight-groups.frontend.store.fight-groups-store.index');
      urlStr += "&page=" + this.data.dataList[this.data.activeName].page;
			
      app._getNetWork({
        url: urlStr,
        data:{
          status: that.data.activeName,
          store_id: that.data.store_id,
          page:that.data.page,
          title: that.data.keywords
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data;
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: true,
              ['dataList[' + that.data.activeName + '].list']: that.data.dataList[that.data.activeName].list.concat(myData.data)
            });
          }else{
            that.setData({
              ['dataList[' + that.data.activeName + '].isLoadMore']: false,
              ['dataList[' + that.data.activeName + '].page']: that.data.dataList[that.data.activeName].page -1
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    let _params ={
      title: '千县千品拼团',
      path: '/packageC/o2o/o2oGroupActivitylist/o2oGroupActivitylist?mid=' + mid
    };
    if(JSON.stringify(this.data.activeItem) != "{}"&&e.from=="button"){
      _params.title= this.data.activeItem.title,
      _params.path= '/packageA/detail_v2/detail_v2?id=' + this.data.activeItem.id + '/'+this.data.store_id+'&name=group'+'&mid=' + mid,
      _params.imageUrl=this.data.activeItem.has_one_goods.thumb;
    }
    this.data.goodshare_info={};
    return _params;

  }
});